<?php
namespace Tbf\Util\ChangeSet;
/**
 * 获取字段变化,请调用Tbf\Util\ChangeSetUtil::getDiff
 * */
/**
 * @deprecated
 */
class Diff
{
    public function __construct($data_old,$data_new)
    {
        $this->data_old = $data_old;
        $this->data_new = $data_new;
        $this->change_old = array();
        $this->change_new = array();
        foreach ($data_old as $k1=>$v1) {
            $this->getOneChange($k1);
        }
        foreach ($data_new as $k1=>$v1) {
            $this->getOneChange($k1);
        }
    }
    /**
     * 获取一个字段改变
     @param string $key
     */
    public function getOneChange($key)
    {
        if (isset($this->change_old[$key])) {
            return;
        }
        if (!isset($this->data_old[$key])) {
            $old = null;
        } else {
            $old = $this->data_old[$key];
        }
        if (!isset($this->data_new[$key])) {
            $new = null;
        } else {
            $new = $this->data_new[$key];
        }
        if ($new===$old) {
            return;
        }
        $this->change_old[$key] = $old;
        $this->change_new[$key] = $new;
    }
    public function getChangeOld()
    {
        return $this->change_old;
    }
    public function getChangeNew()
    {
        return $this->change_new;
    }
    public function isChange()
    {
        if (empty($this->change_old)) {
            return false;
        } else {
            return true;
        }
    }
    /**
     * 根据数据变化获取的修改情况
     * 没有变化返回none
     * 添加返回add
     * 删除返回delete
     * */
    public function getAction()
    {
        $empty_old = empty($this->data_old);
        $empty_new = empty($this->data_new);
        if ($empty_old&&$empty_new) {
            return 'none';
        }
        if ($empty_old) {
            return 'add';
        }
        if ($empty_new) {
            return 'delete';
        }
        if (empty($this->change_old)) {
            return 'none';
        }

        return 'save';
    }
}
